package com.svms.save.importexcel.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ReflectionUtils;

/**
 * 
 * <p>
 * Title : 吉林省公安厅情报平台
 * </p>
 * <p>
 * Description: TODO[导入Excel]_[反射工具]
 * </p>
 * <p>
 * Copyright : Copyright (c) 2014
 * </p>
 * <p>
 * Company : 科大讯飞信息科技股份有限公司
 * </p>
 * <p>
 * Department : 行软事业部 公安情报
 * </p>
 * 
 * @author : 杨旭 xuyang2@iflytek.com
 * @version : 1.0
 */
public class ReflectUtils {
	/**
	 * ClassName获取实例
	 * 
	 * @param em
	 * @return
	 */
	public static Object newInstanceByMapping(String className) {
		Object object = null;
		if (StringUtils.isNotBlank(className)) {
			try {
				object = Class.forName(className).newInstance();
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return object;
	}

	@SuppressWarnings("unchecked")
	public static Method getGetMethod(Class objectClass, String fieldName) {
		StringBuffer sb = new StringBuffer();
		sb.append("get");
		sb.append(fieldName.substring(0, 1).toUpperCase());
		sb.append(fieldName.substring(1));
		try {
			return objectClass.getMethod(sb.toString());
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 执行get方法
	 * 
	 * @param o
	 *            执行对象
	 * @param fieldName
	 *            属性
	 */
	public static Object invokeGet(Object o, String fieldName) {
		Method method = getGetMethod(o.getClass(), fieldName);
		try {
			return method.invoke(o, new Object[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * java反射bean的set方法
	 * 
	 * 
	 * 
	 * @param objectClass
	 * 
	 * @param fieldName
	 * 
	 * @return
	 */

	@SuppressWarnings("unchecked")
	public static Method getSetMethod(Class objectClass, String fieldName) {
		try {
			Class[] parameterTypes = new Class[1];
			Field field = ReflectionUtils.findField(objectClass, fieldName);
			parameterTypes[0] = field.getType();
			StringBuffer sb = new StringBuffer();
			sb.append("set");
			sb.append(fieldName.substring(0, 1).toUpperCase());
			sb.append(fieldName.substring(1));
			Method method = ReflectionUtils.findMethod(objectClass, sb.toString(),parameterTypes);
			return method;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	/**
	 * 执行set方法
	 * 
	 * @param o
	 *            执行对象
	 * @param fieldName
	 *            属性
	 */
	public static Object invokeSet(Object o, String fieldName, Object[] args) {
		Method method = getSetMethod(o.getClass(), fieldName);
		try {
			return method.invoke(o, args);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
